import { ui } from "../ui/layaMaxUI";
import sceneutil from "../utils/sceneutil";
import logger from "../utils/logger";
import gamedata from "../utils/gamedata";
import wxutils from "../utils/wxutils";
import netMgr, { Apis } from "../utils/netMgr";
export default class home extends ui.homeUI {
    public messageArr = ["恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！", "恭喜汪汪汪汪获得大米一袋！"];
    private static _Instance: home;
    public static get Instance(): home {
        if (!this._Instance) {
            logger.log("home尚未初始化");
            this._Instance = new home();
        }
        return this._Instance;
    }
    constructor() {
        super();
        home._Instance = this;

    }
    onEnable() {
        this.main.width = sceneutil.Instance.shejibili.width;
        this.main.height = wxutils.Instance.boxMain.height;
        this.main.y = wxutils.Instance.boxMain.y;

        // this.btntest.on("click", this, () => {
        //     this.gundong();
        // })

        // this.btntest1.on("click", this, () => {
        //     sceneutil.Instance.floatText("测试浮动字", 450, Laya.stage)
        // })
        this.pro.value = 0;
        this.addpro(1);
        this.showButs();
        // this.initBotList();

        // this.btntest.bottom = 0;//   测试代码
        this.initData();

        // 请求收货地址
        // netMgr.Instance.getAdress();
        Laya.stage.on("click", this, () => {
            // console.log("请求收货地址")

            // netMgr.Instance.setAdress();
            // this.gundong();


            // console.log(this.aaa)
            // console.warn(this.aaaa)

            // wxutils.Instance.fenxiang()

            // netMgr.Instance.get(Apis.choujiang, (data) => {
            //     console.log("抽奖---》", data)
            // })
            netMgr.Instance.luckDraw();
        })


        this.changku.on("click", this, () => {
            sceneutil.Instance.showBlackBack();
            Laya.Scene.open("duihuan.scene", false);
        })


        this.shifeizengchan.on("click", this, () => {
            sceneutil.Instance.showBlackBack();
            Laya.Scene.open('shifei.scene', false);
        })

        this.yonghushaidan.on("click", this, () => {
            sceneutil.Instance.showBlackBack();
            Laya.Scene.open('yonghushaidan.scene', false);
        })

        this.yaoqingjiangli.on("click", this, () => {
            sceneutil.Instance.showBlackBack();
            Laya.Scene.open('yaoqingjiangli.scene', false);
        })


    }

    initData() {

        Laya.timer.once(2000, this, this.gundong)
        Laya.timer.once(1000, this, () => {
            sceneutil.Instance.showBlackBack();
            Laya.Scene.open('gongxi.scene', false)
        })
        // this.milinum = gamedata.Instance.mili;//初始化米粒数量
    }



    showButs() {
        // let list = this.main.getChildByName("list") as Laya.List
        for (let i = 1; i < 6; i++) {
            let btn = this.main.getChildByName(i.toString()) as Laya.Button;
            btn.scale(0, 0);
            // btn.bottom = list.height + 50;
            btn.visible = true;
            Laya.timer.once(100 * i + 50, this, () => {
                Laya.Tween.to(btn, { scaleX: 1, scaleY: 1 }, 200, Laya.Ease.backOut)
            })
        }

        // for (let i = 5; i < 8; i++) {
        //     let btn = this.main.getChildByName(i.toString()) as Laya.Button;
        //     Laya.timer.once(100 * i + 50, this, () => {
        //         Laya.Tween.to(btn, { x: 11 }, 200, Laya.Ease.backOut)
        //     })
        // }

        // Laya.timer.once(200, this, () => {
        //     let btn = this.main.getChildByName("5") as Laya.Button;
        //     btn.bottom = 220;
        //     Laya.Tween.to(btn, { x: 560 }, 300, Laya.Ease.backOut);
        // })
        //进度条位置
        // this.pro.bottom = 260;



        // //种子位置s
        // this.zz.anchorX = 0.5;
        // this.zz.anchorY = 0.5;
        // this.zz.x = 360;
        // this.zz.scale(0.8, 0.8);
        // this.zz.bottom = 600;

        // // this.js.top = 300;
        // this.js.x = 900;
        // this.mrkl.x = -200;
        // Laya.timer.once(300, this, () => {
        //     Laya.Tween.to(this.mrkl, { x: 30 }, 300, Laya.Ease.backOut);
        //     Laya.timer.once(200, this, () => {
        //         Laya.Tween.to(this.js, { x: 550 }, 300, Laya.Ease.backOut);
        //     })
        // })

    }

    initBotList() {
        // let back = this.main.getChildByName("list").getChildByName("back") as Laya.Sprite;
        // back.width = 720;
        // let list = this.main.getChildByName("list") as Laya.List;
        // back.height = list.height;
        // list.elasticEnabled = true;

        // list.renderHandler = new Laya.Handler(this, (cell: Laya.Box, index: number) => {

        // })

        // list.mouseHandler = new Laya.Handler(this, (e: Event, index: number) => {

        // })
        // list.dataSource = [{ name: "用户001", img: 'http://asdadasd.png' }, { name: "用户001", img: 'http://asdadasd.png' }, { name: "用户001", img: 'http://asdadasd.png' }];
        // logger.log("列表初始化完毕");
    }



    addpro(v) {
        let speed = 80;
        Laya.Tween.to(this.pro, { value: v }, 80 * 100 * v, null, Laya.Handler.create(this, () => {
            this.pro.value = 0;
            this.addpro(1);
        }))
    }

    //滚动条
    gundong(time?: any) {
        if (this.messageArr.length == 0) {
            Laya.timer.once(time, this, () => {
                logger.log("暂无消息!!");
                Laya.Tween.to(this.lbback, { alpha: 0 }, 200, null, Laya.Handler.create(this, () => {
                    this.lbback.visible = false;
                    this.laba.visible = false;
                    this.lbback.alpha = 100;
                    this.laba.alpha = 100;
                }))
                Laya.Tween.to(this.laba, { alpha: 0 }, 200);
            })
            return;
        }
        this.lbback.visible = true;
        this.laba.visible = true;
        let speed = 0.2;
        let txt = new Laya.Text();
        txt.text = this.messageArr.shift();
        txt.fontSize = 35;
        txt.valign = "middle";
        txt.x = this.pp.width;
        txt.color = "#ffffff";
        txt.y = 0;
        txt.height = this.pp.height;
        this.pp.addChild(txt);
        Laya.Tween.to(txt, { x: -txt.width }, (txt.width + this.pp.width) / speed, null, Laya.Handler.create(this, () => {
            txt.destroy();
        }));
        Laya.timer.once(txt.width / speed + 300, this, this.gundong, [((txt.width + this.pp.width) / speed) - (txt.width / speed + 300)]);
    }



}